#!/bin/bash
set -e

# ./docker_install.sh -host user@host:port 


# 该变量需谨慎！！！ 为当前用户目录下的root_path, 后期执行过程中会删除该目录
root_path='docker'
shell_path='./'
shell_file='install.sh'
# 不可以/结尾
conf_path='conf'
# 不可以/结尾
tools_path='tools'


while [[ $# -ge 1 ]]
do
  case $1 in
    -h) 
      printf "%-10s \t %s" '-h' 'show help document'
      printf "\n"
      printf "%-10s \t %s" '-host' 'e.g. user@host:port'
      printf "\n"
      printf "default install on this server"
      break
      ;;
    -host)
      source ../../utils/upload_shell.sh
      host=$2
      shift 2
      upload_and_exec $@
      break
      ;;
    run)
      # 更新yum源为阿里源
      docker_compose_path=$HOME'/'$root_path'/'$tools_path'/docker-compose'
      cd $root_path
      source ./conf/docker.conf
      sudo mkdir -p $docker_volumes_path $docker_config_path
      # sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
      # CentOS 7
      #sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
      # CentOS 8
      sudo yum install -y wget
      sudo wget -O /etc/yum.repos.d/CentOS-Linux-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
      #sudo yum clean all
      sudo yum makecache
      # step 1: 安装必要的一些系统工具
      sudo yum install -y yum-utils device-mapper-persistent-data lvm2
      # Step 2: 添加软件源信息
      sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
      # Step 3: 更新并安装 Docker-CE
      # sudo yum makecache fast
      sudo yum -y install docker-ce
      sudo mkdir -p /etc/docker
      sudo tee /etc/docker/daemon.json <<EOF
      {
        "registry-mirrors": ["${docker_registry_mirrors}"]
      }
EOF
      echo '==================================================docker info================================================='
      sudo cp $docker_compose_path /usr/local/bin/docker-compose
      sudo chmod +x /usr/local/bin/docker-compose
      sudo ln -s -b /usr/local/bin/docker-compose /usr/bin/docker-compose
      docker-compose --version
      sudo systemctl daemon-reload
      sudo systemctl enable docker.service
      sudo gpasswd -a ${USER} docker
      sudo systemctl restart docker
      sudo docker info
      break
      ;;
    *)
      echo -e "\033[31m Unknow option:$1\033[0m"
			break
			;;
  esac
done
